﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NormReference.Models;
using NormReference.DAL;
using NormReference.Services;

namespace NormReference.Controllers
{ 
    [Authorize]
    public class ComparisonGroupController : Controller
    {
        private NormReferenceContext db = new NormReferenceContext();

        //
        // GET: /ComparisonGroup/

        public ViewResult Index()
        {
          return View(db.groups.ToList());
        }

        //
        // GET: /ComparisonGroup/Details/5

        public ViewResult Details(int id)
        {
            ComparisonGroup comparisongroup = db.groups.Find(id);
            return View(comparisongroup);
        }

        //
        // GET: /ComparisonGroup/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ComparisonGroup/Create

        [HttpPost]
        public ActionResult Create(ComparisonGroup comparisongroup)
        {
            if (ModelState.IsValid)
            {
                db.groups.Add(comparisongroup);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(comparisongroup);
        }
        
        //
        // GET: /ComparisonGroup/Edit/5
 
        public ActionResult Edit(int id)
        {
            ComparisonGroup comparisongroup = db.groups.Find(id);
            return View(comparisongroup);
        }

        //
        // POST: /ComparisonGroup/Edit/5

        [HttpPost]
        public ActionResult Edit(ComparisonGroup comparisongroup)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comparisongroup).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(comparisongroup);
        }

        //
        // GET: /ComparisonGroup/Delete/5
 
        public ActionResult Delete(int id)
        {
            ComparisonGroup comparisongroup = db.groups.Find(id);
            return View(comparisongroup);
        }

        //
        // POST: /ComparisonGroup/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ComparisonGroup comparisongroup = db.groups.Find(id);
            db.groups.Remove(comparisongroup);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

       
        public ActionResult Queue(int id)
        {
            ComparisonGroup comparisongroup = db.groups.Find(id);            
            ComparisonGroupQueue_Backup backup = new ComparisonGroupQueue_Backup();
            //UpdateModel(backup, "ComparisonGroupQueue_Backup");
            Logger.getInstance().printToLogFile("Comparison Group ID " + comparisongroup.ID);
            backup.value = comparisongroup.ID;
            Logger.getInstance().printToLogFile("Backup Group ID in Controller: " + backup.value);
            db.comparisonGroupQueue_Backup.Add(backup);
            //db.comparisonGroupQueue_Backup.Attach(backup);
            db.SaveChanges();
            Logger.getInstance().printToLogFile("Group has been queued.");
            return RedirectToAction("Index");
         }
    }
}